package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.qurey.StrategyQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;




    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyQuery qo){
        //PageInfo  : 分页数据封装对象, Mybatis插件, 操作的是Mysql数据库
        //Page : 分页数据封装对象, spring-data 的组件, 操作是mongodb
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);

        return "strategy/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){

        strategyService.saveOrUpdate(strategy);

        return JsonResult.success();
    }



    @RequestMapping("/input")
    public String input(Model model, String id){
        //修改 带上id
        if(StringUtils.hasLength(id)){
            //strategy
            model.addAttribute("strategy", strategyService.get(id));
        }

        //catalogs
        model.addAttribute("catalogs",strategyCatalogService.groupList());

        //themes
        model.addAttribute("themes", strategyThemeService.list());

        return "strategy/input";
    }


    @GetMapping("/delete")
    @ResponseBody
    private Object delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }


    @GetMapping("/changeState")
    @ResponseBody
    private Object changeState(String id,String state){
        strategyService.changeState(id,state);
        return JsonResult.success();
    }


}
